/*
 * CAD System for seamlessly constructing 2D and 3D scenes with aspects and motion
 * Copyright (C) 2013 Bernhard Haumacher and others
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.haumacher.draw5d.math.d2.constructions.internal;

import static de.haumacher.draw5d.math.d2.D2Computations.*;
import de.haumacher.draw5d.dataflow.Property;
import de.haumacher.draw5d.dataflow.Scope;
import de.haumacher.draw5d.dataflow.core.InputImpl;
import de.haumacher.draw5d.math.d2.D2;
import de.haumacher.draw5d.math.d2.D2Base;
import de.haumacher.draw5d.math.d2.Straight;
import de.haumacher.draw5d.math.d2.constructions.StraightThroughPoints;

/**
 * {@link StraightComputationImpl} that computes the straight that lies on both given points
 * {@link #P1} and {@link #P2}.
 * 
 * @author <a href="mailto:haui@haumacher.de">Bernhard Haumacher</a>
 * @version $Revision: 30 $ $Author: haumacher@googlemail.com $ $Date: 2013-08-09 19:27:26 +0200
 *          (Fri, 09 Aug 2013) $
 */
public class StraightThroughPointsImpl extends StraightComputationImpl implements StraightThroughPoints {

	public static final Property<D2> P1 = property(D2.class);

	public static final Property<D2> P2 = property(D2.class);

	/**
	 * Creates a {@link StraightThroughPointsImpl}.
	 * 
	 * @param scope
	 *        See {@link #getScope()}.
	 */
	public StraightThroughPointsImpl(Scope scope) {
		super(scope, P1, P2);
	}

	@Override
	public void validate() {
		D2 p1 = get(P1);
		D2 p2 = get(P2);

		Straight result = getResult();
		D2Base origin = result.getOrigin();
		D2Base unit = result.getUnit();

		assign(origin, p1);

		sub(unit, p2, p1);
		double length = length(unit);
		multiply(unit, unit, 1 / length);
	}

}
